package com.zenithmind.document.pojo.query;

import com.zenithmind.common.pojo.query.BaseQueryDTO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 文档收藏查询DTO
 * 遵循单一职责原则：专注于收藏查询条件
 * 遵循开闭原则：支持查询条件的扩展
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(description = "文档收藏查询DTO")
public class DocumentFavoriteQueryDTO extends BaseQueryDTO {

    @Schema(description = "文档ID", example = "doc123456")
    private String documentId;

    @Schema(description = "文档ID列表")
    private List<String> documentIds;

    @Schema(description = "用户ID", example = "user123456")
    private String userId;

    @Schema(description = "用户ID列表")
    private List<String> userIds;

    @Schema(description = "收藏夹ID", example = "folder123456")
    private String folderId;

    @Schema(description = "收藏夹ID列表")
    private List<String> folderIds;

    @Schema(description = "收藏夹名称", example = "我的收藏")
    private String folderName;

    @Schema(description = "收藏夹名称关键词", example = "收藏")
    private String folderNameKeyword;

    @Schema(description = "文档标题关键词", example = "需求")
    private String documentTitleKeyword;

    @Schema(description = "文档类型", example = "PDF")
    private String documentType;

    @Schema(description = "文档类型列表")
    private List<String> documentTypes;

    @Schema(description = "文档分类", example = "技术文档")
    private String documentCategory;

    @Schema(description = "文档分类列表")
    private List<String> documentCategories;

    @Schema(description = "标签", example = "重要")
    private String tags;

    @Schema(description = "标签关键词", example = "重要")
    private String tagsKeyword;

    @Schema(description = "备注关键词", example = "学习")
    private String remarksKeyword;

    @Schema(description = "是否公开", example = "true")
    private Boolean isPublic;

    @Schema(description = "状态", example = "ACTIVE", allowableValues = {"ACTIVE", "INACTIVE", "DELETED"})
    private String status;

    @Schema(description = "状态列表")
    private List<String> statuses;

    @Schema(description = "收藏时间开始", example = "2025-01-01T00:00:00")
    private LocalDateTime favoriteTimeStart;

    @Schema(description = "收藏时间结束", example = "2025-01-31T23:59:59")
    private LocalDateTime favoriteTimeEnd;

    @Schema(description = "创建时间开始", example = "2025-01-01T00:00:00")
    private LocalDateTime createTimeStart;

    @Schema(description = "创建时间结束", example = "2025-01-31T23:59:59")
    private LocalDateTime createTimeEnd;

    @Schema(description = "更新时间开始", example = "2025-01-01T00:00:00")
    private LocalDateTime updateTimeStart;

    @Schema(description = "更新时间结束", example = "2025-01-31T23:59:59")
    private LocalDateTime updateTimeEnd;

    @Schema(description = "排序字段", example = "favoriteTime", allowableValues = {
            "favoriteTime", "createTime", "updateTime", "documentTitle"
    })
    private String sortField;

    @Schema(description = "排序方向", example = "DESC", allowableValues = {"ASC", "DESC"})
    private String sortDirection;

    @Schema(description = "分组字段", example = "folderId", allowableValues = {
            "folderId", "documentType", "documentCategory", "status"
    })
    private String groupBy;

    @Schema(description = "是否包含已删除", example = "false")
    private Boolean includeDeleted;

    @Schema(description = "是否只查询公开收藏", example = "false")
    private Boolean onlyPublic;

    @Schema(description = "是否只查询私有收藏", example = "false")
    private Boolean onlyPrivate;

    /**
     * 验证查询条件
     */
    public boolean isValid() {
        // 时间范围验证
        if (favoriteTimeStart != null && favoriteTimeEnd != null) {
            if (favoriteTimeStart.isAfter(favoriteTimeEnd)) return false;
        }
        
        if (createTimeStart != null && createTimeEnd != null) {
            if (createTimeStart.isAfter(createTimeEnd)) return false;
        }
        
        if (updateTimeStart != null && updateTimeEnd != null) {
            if (updateTimeStart.isAfter(updateTimeEnd)) return false;
        }
        
        return true;
    }

    /**
     * 是否有时间范围查询
     */
    public boolean hasTimeRangeQuery() {
        return favoriteTimeStart != null || favoriteTimeEnd != null ||
               createTimeStart != null || createTimeEnd != null ||
               updateTimeStart != null || updateTimeEnd != null;
    }

    /**
     * 是否有关键词查询
     */
    public boolean hasKeywordQuery() {
        return folderNameKeyword != null || documentTitleKeyword != null ||
               tagsKeyword != null || remarksKeyword != null;
    }

    /**
     * 是否有收藏夹查询
     */
    public boolean hasFolderQuery() {
        return folderId != null || (folderIds != null && !folderIds.isEmpty()) ||
               folderName != null || folderNameKeyword != null;
    }

    /**
     * 是否有文档查询
     */
    public boolean hasDocumentQuery() {
        return documentId != null || (documentIds != null && !documentIds.isEmpty()) ||
               documentTitleKeyword != null || documentType != null ||
               (documentTypes != null && !documentTypes.isEmpty()) ||
               documentCategory != null || (documentCategories != null && !documentCategories.isEmpty());
    }

    /**
     * 获取默认排序
     */
    public String getDefaultSort() {
        return "favoriteTime DESC";
    }

    /**
     * 是否查询公开收藏
     */
    public boolean isQueryPublic() {
        return onlyPublic != null && onlyPublic;
    }

    /**
     * 是否查询私有收藏
     */
    public boolean isQueryPrivate() {
        return onlyPrivate != null && onlyPrivate;
    }

    /**
     * 是否为复杂查询
     */
    public boolean isComplexQuery() {
        return hasTimeRangeQuery() || hasKeywordQuery() || hasFolderQuery() || hasDocumentQuery();
    }
}
